﻿using System;
using System.Xml;

namespace WebConfigurationService
{
    class XmlHelper
    {
        public static string Filename { get; } = "Config.xml";

        public static string GetInnerText(XmlDocument xmlDocument, string nodePath)
        {
            return xmlDocument.SelectSingleNode(nodePath).InnerText;
        }

        public static string GetInnerText(XmlNode xmlNode, string nodeName)
        {
            return xmlNode.SelectSingleNode(nodeName).InnerText;
        }

        /// <summary>
        /// 获取Xml文档指定路径节点的内容
        /// </summary>
        /// <param name="nodePath">节点路径</param>
        /// <returns>节点内容</returns>
        public static string GetInnerText(string nodePath)
        {
            XmlDocument config = new();
            config.Load(Filename);
            return config.SelectSingleNode("/configuration/" + nodePath.Trim('/')).InnerText;
        }

        /// <summary>
        /// 设置Xml文档指定路径节点的内容
        /// </summary>
        /// <param name="nodePath">节点路径</param>
        /// <param name="content">节点内容</param>
        public static void SetInnerText(string nodePath, string content)
        {
            XmlDocument config = new();
            config.Load(Filename);
            config.SelectSingleNode("/configuration/" + nodePath.Trim('/')).InnerText = content;
            config.Save(Filename);
        }
    }
}
